{extend name="adminblue/base" /}
{block name="resources"/}
<style>
</style>
{/block}
{block name="main"}
<div class="options-btn">
	<button class="btn-common-delete" onclick="batchDelete()">批量删除</button>
	<button class="btn-common" onclick="location.href='{:__URL('ADMIN_MAIN/config/addHomeNotice')}';">添加首页公告</button>
</div>
<table class="table-class">
	<colgroup>
		<col style="width: 2%">
		<col style="width: 68%;">
		<col style="width: 16%;">
		<col style="width: 4%;">
		<col style="width: 10%">
	</colgroup>
	<thead>
		<tr align="center">
			<th><i class="checkbox-common"><input type="checkbox" onClick="CheckAll(this)" id="chek_all"></i></th>
			<th align="left">公告标题</th>
			<th>创建时间</th>
			<th>排序</th>
			<th>操作</th>
		</tr>
	</thead>
	<tbody></tbody>
</table>
<script>
function LoadingInfo(page_index) {
	
	$.ajax({
		type : "post",
		url : "{:__URL('ADMIN_MAIN/config/userNotice')}",
		data : { "page_index" : page_index, "page_size" : $("#showNumber").val() },
		success : function(data) {
			if (data["data"].length > 0) {
				$(".table-class tbody").empty();
				for (var i = 0; i < data["data"].length; i++) {
					var html = '';
					html += '<tr align="center">';
					html += '<td><i class="checkbox-common"><input name="sub" type="checkbox" value="'+ data['data'][i]['id']+'"></i></td>';
					
					html += '<td align="left">'+data["data"][i]["notice_title"]+'</td>';
					html += '<td>' + timeStampTurnTime(data["data"][i]["create_time"])+ '</td>';
					html += '<td><input type="number" min="0" value="'+ data["data"][i]["sort"]+'" class="input-common input-common-sort" data-id="'+ data['data'][i]['id']+'"></td>';
					html += '<td><a href="'+__URL('ADMIN_MAIN/config/updateHomeNotice?id='+ data['data'][i]['id'])+'">修改</a><a style="cursor: pointer;" onclick="deleteNotice('+data['data'][i]['id']+')">删除</a></td> ';
					html += '</tr>';
					$(".table-class tbody").append(html);
				}
			} else {
				var html = '<tr align="center"><td colspan="8">暂无公告信息</td></tr>';
				$(".table-class tbody").html(html);
			}
			initPageData(data["page_count"],data['data'].length,data['total_count']);
			$("#pageNumber").html(pagenumShow(jumpNumber,$("#page_count").val(),{$pageshow}));
			load();
		}
	});
}
//删除公告
function deleteNotice(id){
	$( "#dialog" ).dialog({
		buttons: {
			"确定": function() {
				$(this).dialog('close');
				$.ajax({
					type : "post",
					url : "{:__URL('ADMIN_MAIN/config/deleteNotice')}",
					data : { "id" : id.toString() },
					success : function(data) {
						if (data["code"] > 0) {
							LoadingInfo(getCurrentIndex(id,'.table-class tbody'));
							$('#chek_all').prop('checked',false);
							showTip(data["message"],'success');
						}else{
							showTip(data["message"],'error');
						}
					}
				})
			},
			"取消,#f5f5f5,#666": function() {
				$(this).dialog('close');
			}
		},
	contentText:"是否删该条公告？",
	});
}
//批量删除公告
function batchDelete() {
	var ids= new Array();
	$(".table-class tbody input[type='checkbox']:checked").each(function() {
		if (!isNaN($(this).val())) {
			ids.push($(this).val());
		}
	});
	if(ids.length ==0){
		$( "#dialog" ).dialog({
			buttons: {
				"确定,#0059d6,#fff": function() {
					$(this).dialog('close');
				}
			},
			contentText:"请选择需要操作的记录",
			title:"消息提醒",
		});
		return false;
	}
	deleteNotice(ids);
}

/*修改排序*/
function load(){
	$('.input-common-sort').change(function(){
		var id = $(this).attr('data-id');
		var sort = $(this).val();
		$.ajax({
			type:"post",
			url:"{:__URL('ADMIN_MAIN/config/modifyNoticeSort')}",
			data:{'id':id,'sort':sort},
			success: function (data) {
				if(data['code'] > 0){
					showTip(data["message"],'success');
				}else{
					showTip(data["message"],'error');
				}
			}
		}); 
	});
}

//全选
function CheckAll(event){
	var checked = event.checked;
	$(".table-class tbody input[type = 'checkbox']").prop("checked",checked);
	if(checked) $(".table-class tbody input[type = 'checkbox']").parent().addClass("selected");
	else $(".table-class tbody input[type = 'checkbox']").parent().removeClass("selected");
}
</script>
{/block}